﻿
@{
    Layout = null;
}

<div>
    <div class="col-3 pr-0" style="display: none">
        <label id="SAMPLE-SelectSampleGroup-Parent-ModalID">@ViewData["ModalID"]</label>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0 pl-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">样品组类型</span>
        </div>
        <div class="col-9 pl-0" style="padding-right: 0px;">
            <select id="SAMPLE-SelectSampleGroup-Type" class="form-control font-rfs border-radius-noleft"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0 pl-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">样品组</span>
        </div>
        <div class="col-9 pl-0" style="padding-right: 0px;">
            <select id="SAMPLE-SelectSampleGroup-Group" class="form-control font-rfs border-radius-noleft"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0 pl-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">批次号</span>
        </div>
        <div class="col-9 pl-0" style="padding-right: 0px;">
            <input id="SAMPLE-SelectSampleGroup-Batch" class="form-control font-rfs border-radius-noleft border-radius-noright" />
        </div>
    </div>
</div>
<div class="table-responsive-sm modal-table-shadow p-0 bg-white rounded col-md-12 pre-scrollable" style="height: 60vh;">
    <table id="SAMPLE-SelectSampleGroup-table" class="table table-striped table-bordered-modal table-sm table-hover">
        <thead>
            <tr>
                <th>ID</th>
                <th></th>
                <th>名称</th>
                <th>描述</th>
                <th>数量</th>
            </tr>
        </thead>
        <tbody style="font-size: calc(0.6em + 0.1vw) !important"></tbody>
    </table>
</div>
<div class="text-right" style="padding-top: 5px;">
    <button id="SAMPLE-SelectSampleGroup-confirm" type="button" class="btn btn-primary font-rfs">确定</button>
    <button id="SAMPLE-SelectSampleGroup-cancel" type="button" class="btn btn-info font-rfs">取消</button>
</div>

<script>
    $(document).ready(function () {
        LoadSampleGroupTypeList('SAMPLE-SelectSampleGroup-Type');
    });

    $("#SAMPLE-SelectSampleGroup-Type").change(function () {
        LoadSampleGroupList('SAMPLE-SelectSampleGroup-Group', $("#SAMPLE-SelectSampleGroup-Type").find("option:selected").text());
    });

    $("#SAMPLE-SelectSampleGroup-Group").change(function () {
        LoadSampleGroup();
    });

    function LoadSampleGroup() {
        var columns = [{ field: "sampleId", visible: false }, { field: "selectItem", width: "5%", align: "center", checkbox: true },
        { field: "name" }, { field: "description" }, { field: "sampleNumber", width: "5%", align: "center", formatter: SetNumber }];

        $('#SAMPLE-SelectSampleGroup-table').bootstrapTable('destroy');
        $('#SAMPLE-SelectSampleGroup-table').bootstrapTable({
            method: 'post',
            url: "GetSamplesBySGID",
            dataType: "json",  // 服务器端返回值类型
            contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
            queryParams: function (params) {
                var data = {
                    SampleGroupId: $("#SAMPLE-SelectSampleGroup-Group").val(),
                };
                return data;
            }, //参数
            queryParamsType: "limit", //参数格式,发送标准的RESTFul类型的参数请求
            dataField: "rows",

            uniqueId: "sampleId",          // 设置主键
            sidePagination: "server", // 服务器端分页，这个参数必需设置
            pagination: true,  // 显示分页条,设为true，启用了分页功能
            pageSize: 20,   // 默认页大小
            pageList: [10, 15, 20, 30],  // 控制每页数目
            locale: "zh-CN",
            columns: columns,          // 加载列定义, 注意！这个地方绑定的列名称开头大写字母会全部变成小写。
            checkboxHeader: true, // 标题行显示全选按钮
            onClickRow: function (row, $element, field) {
                $("#SAMPLE-SelectSampleGroup-table").find("tbody").find("tr").each(function () {
                    $(this).removeClass('changeblueColor');
                });
                $element.addClass("changeblueColor");
            },
            silent: true,  //刷新事件必须设置
            formatLoadingMessage: function () {
                return "请稍等，正在加载中...";
            },
            formatNoMatches: function () {  //没有匹配的结果
                return '无符合条件的记录';
            },
            onLoadError: function (data) {
                $('#SAMPLE-SelectSampleGroup-table').bootstrapTable('removeAll');
            },
            onLoadSuccess: function (data) {   // 数据加载成功后触发该事件
                SetBootstrapStyle();
            },
            formatShowingRows: function (pageFrom, pageTo, totalRows) {
                return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
            },
            formatRecordsPerPage: function (pageNumber) {
                return " 每页显示" + pageNumber + '条记录';
            }
        });
    }

    function SetNumber(value, row, index) {
        var strHtml = "<input class='table-cell-input' onchange='SetSampleGroupNumber(this)' value='" + value + "'/>";
        return strHtml;
    }

    function SetSampleGroupNumber(obj) {
        var sid = $(obj).parent().parent().attr('data-uniqueid');
        var row = $('#SAMPLE-SelectSampleGroup-table').bootstrapTable('getRowByUniqueId', sid);
        row.sampleNumber = $(obj).val();
        $('#SAMPLE-SelectSampleGroup-table').bootstrapTable('updateByUniqueId', row);
    }

    $("#SAMPLE-SelectSampleGroup-cancel").click(function (event) {
        var modalID = $("#SAMPLE-SelectSampleGroup-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });

    $("#SAMPLE-SelectSampleGroup-confirm").click(function () {
        var SampleList = [];
        var rows = $('#SAMPLE-SelectSampleGroup-table').bootstrapTable('getSelections');
        $(rows).each(function () {
            var item = this.sampleId + '.' + this.sampleNumber;
            SampleList.push(item);
        })

        if (SampleList.length > 0) {
            $.post("AddSampleLogin", {
                BatchId: $('#SAMPLE-SelectSampleGroup-Batch').val(),
                SampleDefinitionList: SampleList.toString()
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $("#SAMPLE-SelectSampleGroup-cancel").click();
                } else {
                    alert(data);
                }
            });
        }
    });

</script>
